// pages/coupon/coupon.js
var httpUtil = require('../../utils/httpUtil.js');
var util = require('../../utils/util.js');

var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    clientHeight:0,
    list:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    var currentTab = options.currentTab;
     that.setData({
      currentTab: currentTab == null ? 2 : currentTab
    })
 

  },
  getList: function (id) {
    var that = this;
    httpUtil.getHttp("/order/list?userId="+that.data.userInfo.id, {}, function (res) {
      that.setData({
        list: res
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;

    var userInfo = util.getKey("userInfo");
    that.setData({
      userInfo: userInfo
    })
    that.getList(userInfo);
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          clientHeight: res.windowHeight
        });
      }
    }); 
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.currentTarget.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.currentTarget.dataset.current
      })
    }
  },
  bindChange: function (e) {

    var that = this;
    that.setData({ currentTab: e.detail.current });

  },
  lookVisa:function(e){
    var orderNumber = e.target.dataset.order;

    wx.navigateTo({
      url: '../goEvaluate/goEvaluate?orderNumber=' + orderNumber
    })
  },
  looktransaction:function(){
    wx.navigateTo({
      url: '../scheduleLook/scheduleLook'
    })
  },
  jzprogress:function(e){
    var orderNumber = e.target.dataset.order;
    if (orderNumber != null && orderNumber != '' && orderNumber!=undefined){

    wx.navigateTo({
      url: '../schedule/schedule?orderNumber=' + orderNumber
    })
    }

  },  
  refund: function(e) {
    var orderNumber = e.target.dataset.order;
    if (orderNumber != null && orderNumber != '' && orderNumber != undefined) {

      wx.navigateTo({
        url: '../refund/refund?orderNumber=' + orderNumber
      })
    }

  },
  looklogistics:function(e){
    var orderNumber = e.target.dataset.order;

    wx.navigateTo({
      url: '../logistics/logistics?orderNumber=' + orderNumber
    }) 
  }, saveGoods:function(e){
    var orderNumber = e.target.dataset.order;
    var that=this;
    httpUtil.getHttp("/order/set/" + orderNumber + "?userId=" + that.data.userInfo.id, {}, function (res) {
      that.getList();
    })
  },
  showDetail:function(e){
    var id = e.target.dataset.id;
    var goodsType = e.target.dataset.type;
    console.log("进入");
    console.log(id);
    console.log(goodsType);

    if (id == undefined) {
      return;
    }

    if (goodsType == undefined) {
      return;
    }

    if (goodsType != undefined && goodsType == 4) {
      wx.navigateTo({
        url: '../detail/detail?id=' + id
      })
      return
    }
    if (goodsType != undefined && goodsType == 0) {
      wx.navigateTo({
        url: '../visaInfo/visaInfo?id=' + id
      })
      return
    }
    wx: wx.navigateTo({
      url: '../proInfo/proInfo?id=' + id,
    })
    },
     pay:function(e){
       var that=this;
       var userInfo = that.data.userInfo;
       var orderNumber = e.target.dataset.order;
       httpUtil.postHttp("/order/pay/"+orderNumber+"?userId=" + userInfo.id 
         , {}, function (res) {
           wx.requestPayment({
             'timeStamp': res.data.timeStamp,
             'nonceStr': res.data.nonceStr,
             'package': res.data.package,
             'signType': res.data.signType,
             'paySign': res.data.paySign,
             'success': function (res) {
               wx.showToast({
                 title: "支付成功",
                 icon: 'loading',
                 duration: 500
               });
               wx.redirectTo({
                 url: '../paySuccess/paySuccess'
               })             },
             'fail': function (res) {
               console.log(res);
               wx.showToast({
                 title: "取消支付",
                 icon: 'loading',
                 duration: 500
               });

             }
           })
         })
     }
  
})